package net.solvetheriddle.wwclient;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import net.solvetheriddle.wwclient.entity.Project;
import net.solvetheriddle.wwclient.entity.User;
import net.solvetheriddle.wwclient.entity.Work;
import android.util.Log;

/**
 * @author anoniim
 * 
 */
public class WWLogic {

  public static final int NONPARAMETER_REQUEST = 0;
  public static final int LOGIN_REQUEST = 1;
  public static final int GET_ALL_PROJECTS_REQUEST = 2;
  public static final int GET_OPEN_PROJECTS_REQUEST = 3;
  public static final int LOGOUT_REQUEST = 4;

  private static User user = null;
  private static Work currentWork = null;

  public WWLogic() {
  }

  /**
   * Check if the parameters match to a user saved in server database. Uses a
   * httpConnection, handles the answer. Parses XML files.
   * 
   * @param company
   *          String set by user
   * @param username
   *          String set by user
   * @param password
   *          String set by user
   * @return Authenticated User or null if login was incorrect
   * @throws IOException
   * @throws ParsingException
   */
  public static User loginUser(String company, String username, String password)
      throws IOException {
    InputStream userLoginResponse = WWServer.loginUser(company, username,
	password);
    user = WWParser.parseUserFromStream(userLoginResponse);
    if (user != null) {
      Log.d("WWCLIENT", "*** Logged as " + user + " (id: " + user.getId() + ")");
      return user;
    }
    return null;
  }

  /**
   * Log the user out destroying his session on server.
   * 
   * @return
   * @throws IOException
   * @throws ParsingException
   */
  public static boolean logout() throws IOException {
    if (WWServer.logoutUser()) {
      user = null;
      Log.d("WWCLIENT", "*** User " + user + " is logged out");
      return true;
    } else {
      Log.e("WWCLIENT", "! Unable to logout user: " + user);
      return false;
    }
  }

  public synchronized static void setCurrentWork(Work newWork) {
    currentWork = newWork;
  }

  public synchronized static void finishCurrentWork() {
    currentWork = null;
  }

  public synchronized static Work getCurrentWork() {
    return currentWork;
  }

  public synchronized static boolean workInProgress() {
    return currentWork != null;
  }

  /**
   * Loads all projects from server.
   * 
   * @return List of projects
   * @throws IOException
   */
  public static List<Project> loadProjectsFromServer() throws IOException {
    InputStream projectsResponse = WWServer.loadProjects();
    List<Project> projects = WWParser.parseProjectsFromStream(projectsResponse);
    return projects;
  }

  /**
   * Gets all work of given project.
   * 
   * @deprecated bere se z databaze
   * @param projectId
   * @return
   * @throws IOException
   * @throws ParsingException
   */
  @Deprecated
  public static List<Work> loadWorkOnProjectFromServer(long projectId)
      throws IOException {
    InputStream workOnProjectResponse = WWServer.loadWorkOnProject(projectId);
    return WWParser.parseWorkFromStream(workOnProjectResponse);
  }

  /**
   * Gets all work between <code>beginTime</code> and <code>endTime</code>.
   * 
   * @param beginTime
   *          time in millis
   * @param endTimetime
   *          in millis
   * @return List of work
   * @throws IOException
   * @throws ParsingException
   */
  public static List<Work> loadWorkFromServer(long beginTime, long endTime)
      throws IOException {
    InputStream workResponse = WWServer.loadWork(beginTime, endTime);
    return WWParser.parseWorkFromStream(workResponse);
  }

  /**
   * @param notSentWork
   *          Works to be sent and saved on Server.
   * @return
   * @throws IOException
   */
  public static long sendFinishedWorkToServer(Work notSentWork)
      throws IOException {
    InputStream incommingStream = WWServer.saveWork(notSentWork);
    long workId = Long.valueOf(WWParser.parseStringFromStream(incommingStream)
	.replaceAll("\\n", ""));
    Log.d("WWCLIENT", "*** new Work inserted under ID: " + workId);
    return workId;
  }

  public static long getCurrentMillis() {
    return System.currentTimeMillis();
  }

  public static long getLastMidnightMillis() {
    Date midnight = new Date();
    midnight.setHours(0);
    midnight.setMinutes(0);
    midnight.setSeconds(0);
    long millis = midnight.getTime();
    return millis;
  }

}
